class Solution {
public:
int test(vector<int>& nums,int k)
{
    vector<int>arr(20001,0);
    int sum=0,num=0;
    for(int l=0,r=0;r<nums.size();r++)
    {
        if(arr[nums[r]]++==0)
        sum++;
        while(sum>k)
        {
            if(arr[nums[l++]]--==1)
            sum--;
        }
        num+=r-l+1;
    }
    return num;
}


    int subarraysWithKDistinct(vector<int>& nums, int k) {
        return test(nums,k)-test(nums,k-1);
    }
};